package com.logos.musicframe.controller;

import com.google.common.base.Preconditions;
import com.logos.musicframe.req.UsersReq;
import com.logos.musicframe.resp.UsersResp;
import com.logos.musicframe.result.Result;
import com.logos.musicframe.service.UsersService;
import com.logos.musicframe.task.SongTask;
import com.logos.musicframe.utils.LocalSessionStorageUtil;
import com.logos.musicframe.utils.UserInfoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 用户信息表(Users)表控制层
 *
 * @author rose
 * @since 2025-06-01 21:06:01
 */
@Component
@Slf4j
public class UsersController {
    /**
     * 服务对象
     */
    @Autowired
    private UsersService usersService;

    @Autowired
    private UserInfoUtil userInfoUtil;
    
    @Autowired
    private SongTask songTask;

    /**
     * 登录请求
     */
    public Result<UsersResp> login(UsersReq usersReq) {
        Preconditions.checkNotNull(usersReq.getUsername(), "用户名不能为空");
        Preconditions.checkNotNull(usersReq.getPassword(), "密码不能为空");
        UsersResp result = usersService.login(usersReq);
        
        // 登录成功后，重新获取推荐歌曲
        if (result != null) {
            // 异步执行，避免影响登录响应速度
            new Thread(() -> {
                try {
                    songTask.refreshRecommendSongs();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
        }
        return Result.ok(result);
    }

    public Result<Boolean> register(UsersReq usersReq) {
        Preconditions.checkNotNull(usersReq.getPhone(), "手机号不能为空");
        Preconditions.checkNotNull(usersReq.getPassword(), "密码不能为空");
        Boolean result = usersService.register(usersReq);
        return Result.ok(result);
    }

    /**
     * 获取当前用户信息
     */
    public Result<UsersResp> getCurrentUserInfo(){
        String[] loginSession = LocalSessionStorageUtil.getLoginSession();
        if (loginSession == null) {
            return Result.fail("请先登录");
        }
        log.info("UserController.getCurrentUserInfo.localSession:{}",loginSession);
        UsersResp usersResp = usersService.queryById(Integer.valueOf(loginSession[0]));
        return Result.ok(usersResp);
    }
    
    /**
     * 更新用户信息
     */
    public Result<Boolean> updateUserInfo(UsersReq usersReq) {
        String[] loginSession = LocalSessionStorageUtil.getLoginSession();
        if (loginSession == null) {
            return Result.fail("请先登录");
        }
        // 确保更新的是当前登录用户
        usersReq.setId(Integer.parseInt(loginSession[0]));
        Boolean result = usersService.updateUserInfo(usersReq);
        return Result.ok(result);
    }
    
    /**
     * 绑定手机号
     */
    public Result<Boolean> bindPhone(UsersReq usersReq) {
        String[] loginSession = LocalSessionStorageUtil.getLoginSession();
        if (loginSession == null) {
            return Result.fail("请先登录");
        }
        // 取出当前用户
        usersReq.setId(Integer.parseInt(loginSession[0]));
        // 验证手机号和验证码
        Preconditions.checkNotNull(usersReq.getPhone(), "手机号不能为空");
        Preconditions.checkNotNull(usersReq.getCode(), "验证码不能为空");
        
        // 调用服务绑定手机号
        Boolean result = usersService.bindPhone(usersReq);
        return Result.ok(result);
    }
    
    /**
     * 发送手机验证码
     * @param phone 手机号
     * @return 是否发送成功
     */
    public Result<Boolean> sendPhoneCode(String phone) {
        Preconditions.checkNotNull(phone, "手机号不能为空");
        Boolean result = usersService.sendPhoneCode(phone);
        return Result.ok(result);
    }
    
    /**
     * @param phone 手机号
     * @return 验证码
     */
    public String getLastSentCode(String phone) {
        return usersService.getLastSentCode(phone);
    }
}

